import Vue from 'vue'
import App from './App'
import router from './router'


Vue.config.productionTip = false

import myConfig from "./common/myConfig";
Vue.prototype.myConfig = myConfig

import axios from "axios";
axios.defaults.baseURL = myConfig.url
Vue.prototype.axios = axios
Vue.prototype.$axios = axios

import ele from 'element-ui'
import 'element-ui/lib/theme-chalk/index.css'

import 'mavon-editor/dist/css/index.css'
import mdEditor from 'mavon-editor'
Vue.use(mdEditor)
Vue.use(ele)

import moment from "moment"
Vue.filter('formatDate', function(value) {
  return moment(value).format('YYYY-MM-DD')
})

import * as echarts from 'echarts'  //引入Echarts，
Vue.prototype.$echarts = echarts  //定义为全局变量
/* eslint-disable no-new */
new Vue({
  el: '#app',
  router,
  components: { App },
  template: '<App/>'
})


router.beforeEach((to,from,next) => {
  if (to.path.startsWith('/login')){
    next()
  } else {
    let admin = JSON.parse(window.localStorage.getItem("userInfo"))
    if (admin == null){
      next({path: '/login'})
    } else {
      //校验token合法性
      axios({
        url: '/checkToken',
        method: 'get',
        headers:{
          token: admin.token
        }
      }).then(res => {
        if (!res.data){
          console.log("校验失败")
          next({path: '/login'})
        }
      })
      next()
    }
  }
})
